<template>
  <div class="article-container">
    <el-card class="search-card">
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-form ref="searchFormRef" :model="searchForm" label-width="80px">
        <el-form-item label="状态：">
          <el-radio-group v-model="searchForm.status">
            <el-radio label="">全部</el-radio>
            <el-radio :label="0">草稿</el-radio>
            <el-radio :label="1">待审核</el-radio>
            <el-radio :label="2">审核通过</el-radio>
            <el-radio :label="3">审核失败</el-radio>
            <el-radio :label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道：">
          <el-select v-model="searchForm.channel_id" placeholder="请选择频道" clearable>
            <el-option :label="item.name" :value="item.id" v-for="item in channelsInfo" :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="日期：">
          <el-date-picker v-model="searchForm.begin_pubdate" type="date" placeholder="开始日期" format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
          <span class="demonstration"> 至 </span>
          <el-date-picker v-model="searchForm.end_pubdate" type="date" placeholder="结束日期" format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="searchAriticles">筛选</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="content-card">
      <div slot="header" class="clearfix">
        根据筛选条件共查询到{{total}}条结果：
      </div>
      <el-table :data="articleList" style="width: 100%" :header-cell-style="{textAlign: 'center'}"
        :cell-style="{ textAlign: 'center' }" v-loading="loading">
        <el-table-column label="封面">
          <template slot-scope="scope">
            <el-image style="width: 100px; height: 100px" :src="scope.row.cover.images[0]" fit="cover" lazy>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题">
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag :type="articleStatus[scope.row.status].type">
              {{articleStatus[scope.row.status].text}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="编辑" placement="top-start">
              <el-button type="primary" icon="el-icon-edit" circle @click="editArticle(scope.row.id)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除" placement="top-start">
              <el-button type="danger" icon="el-icon-delete" circle @click="deleteArticleInfo(scope.row.id)">
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="block">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page="searchForm.page" :page-sizes="[10, 20, 30, 40]" :page-size="searchForm.per_page"
          layout="total, sizes, prev, pager, next, jumper" :total="total" :disabled="loading">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>
<script>
  import {
    getArticlesInfo,
    getAllChannelsInfo,
    deleteArticleInfo
  } from '@/api/article.js'
  export default {
    name: 'Article',
    data() {
      return {
        loading: false,
        //频道信息
        channelsInfo: [],
        //搜索参数
        searchForm: {
          status: '',
          channel_id: '',
          begin_pubdate: '',
          end_pubdate: '',
          page: 1,
          per_page: 20,
          response_type: ''
        },
        //获得的文章信息
        articleList: [],
        total: 0,
        articleStatus: [{
          status: 0,
          text: '草稿',
          type: 'info'
        }, {
          status: 1,
          text: '待审核',
          type: 'primary'
        }, {
          status: 2,
          text: '审核通过',
          type: 'success'
        }, {
          status: 3,
          text: '审核失败',
          type: 'danger'
        }, {
          status: 4,
          text: '已删除',
          type: 'warning'
        }]
      }
    },
    created() {
      this.getAllChannelsInfo()
      this.searchAriticles()
    },
    mounted() {

    },
    methods: {
      getAllChannelsInfo() {
        getAllChannelsInfo().then(res => {
          this.channelsInfo = res.data.data.channels
        })
      },
      searchAriticles() {
        this.searchForm.page = 1
        this.loading = true
        getArticlesInfo(this.searchForm).then(res => {
          const {
            results,
            total_count
          } = res.data.data
          this.articleList = results
          this.total = total_count
          this.loading = false
        })
      },
      deleteArticleInfo(id) {
        this.$confirm('是否确定删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteArticleInfo(id.toString()).then(res => {
            this.loading = true
            if (this.articleList.length === 1) {
              this.searchForm.page = this.searchForm.page - 1
            }
            getArticlesInfo(this.searchForm).then(res => {
              const {
                results,
                total_count
              } = res.data.data
              this.articleList = results
              this.total = total_count
              this.loading = false
              this.$message({
                type: 'success',
                message: '删除成功'
              })
            })
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      handleSizeChange(val) {
        this.loading = true
        this.searchForm.per_page = val
        this.searchForm.page = 1
        getArticlesInfo(this.searchForm).then(res => {
          const {
            results,
            total_count
          } = res.data.data
          this.articleList = results
          this.total = total_count
          this.loading = false
        })
      },
      handleCurrentChange(val) {
        this.loading = true
        this.searchForm.page = val
        getArticlesInfo(this.searchForm).then(res => {
          const {
            results,
            total_count
          } = res.data.data
          this.articleList = results
          this.total = total_count
          this.loading = false
        })
      },
      editArticle(id) {
        this.$router.push('/publish?id=' + id)
      }
    }
  }

</script>
<style lang="less" scoped>
  .search-card,
  .content-card {
    margin: 15px;
  }

</style>
